<template>
  <div class="page-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>模型管理</span>
          <el-button type="primary" @click="handleAdd">新增模型</el-button>
        </div>
      </template>
      
      <el-table :data="tableData" border stripe v-loading="loading">
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="provider" label="提供商" />
        <el-table-column prop="kind" label="类型" />
        <el-table-column prop="enabled" label="状态" width="80">
          <template #default="{ row }">
            <el-tag :type="row.enabled ? 'success' : 'danger'">
              {{ row.enabled ? '启用' : '禁用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150">
          <template #default="{ row }">
            <el-button link type="primary" @click="handleEdit(row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 新增/编辑对话框 -->
    <el-dialog 
      v-model="dialogVisible" 
      :title="dialogTitle"
      width="600px"
    >
      <el-form :model="form" label-width="100px">
        <el-form-item label="模型名称">
          <el-input v-model="form.name" placeholder="请输入模型名称" />
        </el-form-item>
        <el-form-item label="提供商">
          <el-select v-model="form.provider" placeholder="请选择提供商">
            <el-option label="Qwen" value="qwen" />
            <el-option label="OpenAI" value="openai" />
            <el-option label="Claude" value="claude" />
            <el-option label="其他" value="other" />
          </el-select>
        </el-form-item>
        <el-form-item label="模型类型">
          <el-select v-model="form.kind" placeholder="请选择类型">
            <el-option label="对话模型" value="dialog" />
            <el-option label="嵌入模型" value="embedding" />
          </el-select>
        </el-form-item>
        <el-form-item label="描述">
          <el-input 
            v-model="form.description" 
            type="textarea" 
            :rows="3"
            placeholder="请输入模型描述"
          />
        </el-form-item>
        <el-form-item label="API Key">
          <el-input 
            v-model="form.api_key" 
            type="password"
            placeholder="请输入API Key"
            show-password
          />
        </el-form-item>
        <el-form-item label="是否启用">
          <el-switch v-model="form.enabled" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit" :loading="submitting">
          确定
        </el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { getModels, createModel, updateModel } from '@/api/admin'

const loading = ref(false)
const tableData = ref([])
const dialogVisible = ref(false)
const dialogTitle = ref('')
const submitting = ref(false)
const editingId = ref(null)

const form = reactive({
  name: '',
  provider: '',
  kind: '',
  description: '',
  api_key: '',
  enabled: true
})

const resetForm = () => {
  form.name = ''
  form.provider = ''
  form.kind = ''
  form.description = ''
  form.api_key = ''
  form.enabled = true
  editingId.value = null
}

const loadData = async () => {
  try {
    loading.value = true
    const res = await getModels({ page: 1, page_size: 20 })
    tableData.value = res.data.items
  } catch (error) {
    console.error(error)
    ElMessage.error('加载数据失败')
  } finally {
    loading.value = false
  }
}

const handleAdd = () => {
  resetForm()
  dialogTitle.value = '新增模型'
  dialogVisible.value = true
}

const handleEdit = (row) => {
  resetForm()
  dialogTitle.value = '编辑模型'
  editingId.value = row.id
  
  // 填充表单数据
  form.name = row.name
  form.provider = row.provider
  form.kind = row.kind
  form.description = row.description || ''
  form.api_key = row.api_key || ''
  form.enabled = row.enabled
  
  dialogVisible.value = true
}

const handleSubmit = async () => {
  try {
    submitting.value = true
    
    if (editingId.value) {
      // 编辑
      await updateModel(editingId.value, form)
      ElMessage.success('更新成功')
    } else {
      // 新增
      await createModel(form)
      ElMessage.success('创建成功')
    }
    
    dialogVisible.value = false
    loadData()
  } catch (error) {
    console.error(error)
    ElMessage.error(error.response?.data?.detail || '操作失败')
  } finally {
    submitting.value = false
  }
}

onMounted(() => {
  loadData()
})
</script>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>

